const {app,BrowserWindow,ipcMain} = require('electron')
const path = require('path')
const fs = require('fs')

const saveFile = (a,b,c)=>{
    console.log('=====>a',a)
    console.log('=====>b',b)
    console.log('=====>c',c)
    // fs.appendFileSync('D:/tmp/hello.txt',b)
    fs.writeFileSync('D:/tmp/hello.txt',b)
}

const readFile = (a,b,c)=>{
    console.log('=====>a',a)
    console.log('=====>b',b)
    console.log('=====>c',c)
    return fs.readFileSync('D:/tmp/hello.txt').toString()
}


function createWindow(){
    const win = new BrowserWindow({
        width:800,
        height:600,
        autoHideMenuBar:true,
        webPreferences:{
            // 预加载脚本
            preload:path.resolve(__dirname,"./preload.js")
        }
    });

    ipcMain.on("save-file",saveFile);
    ipcMain.handle("read-file",readFile);

    // win.loadURL("https://www.bilibili.com/")
    win.loadFile("./src/index.html")
}

app.whenReady().then(() => {
    console.log('started ...');
    createWindow()  
    app.on('activate', () => {
      if (BrowserWindow.getAllWindows().length === 0) createWindow()
    })
})

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
})

